package com.zb.frame.base.upgrade;

import com.zb.frame.base.upgrade.common.UpgradeHelper;
import com.zb.frame.base.upgrade.enums.DBType;
import com.zb.frame.base.upgrade.enums.ValueType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

public class UpgradeTable {

    private static final Logger log = LoggerFactory.getLogger(UpgradeTable.class);

    private final String name;
    private final String comment;
    private final List<UpgradeColumn> columnList = new ArrayList();
    private final List<String> primaryKeyList = new ArrayList();
    private final List<UpgradeIndex> indexList = new ArrayList();
    private final List<String> identityKeyList = new ArrayList();

    public UpgradeTable(String name, String comment) {
        this.name = name;
        this.comment = comment;
    }

    public void addColumns(UpgradeColumn... columns) {
        this.columnList.addAll(Arrays.asList(columns));
    }

    public void addPrimaryKeys(String... columnNames) {
        this.primaryKeyList.addAll(Arrays.asList(columnNames));
    }

    public void addIndexs(UpgradeIndex... indexs) {
        this.indexList.addAll(Arrays.asList(indexs));
    }

    public void addIdentityKeys(String... columnNames) {
        this.identityKeyList.addAll(Arrays.asList(columnNames));
    }

    public void doCreateTable(Connection conn, DBType type) throws SQLException {
        Integer listLen = this.columnList.size();
        String[] columnNames = new String[listLen];
        String[] columnComments = new String[listLen];
        ValueType[] columnTypes = new ValueType[listLen];
        boolean[] columnNullAbles = new boolean[listLen];
        String[] primaryKeys = this.primaryKeyList.toArray(new String[this.primaryKeyList.size()]);
        Integer[] columnLengths = new Integer[listLen];
        String[] identityKeys = this.identityKeyList.toArray(new String[this.primaryKeyList.size()]);

        for(int i = 0 , len = listLen; i < len; ++i) {
            UpgradeColumn column = (UpgradeColumn)this.columnList.get(i);
            columnNames[i] = column.getName();
            columnComments[i] = column.getComment();
            columnTypes[i] = column.getType();
            columnNullAbles[i] = column.isNullable();
            columnLengths[i] = column.getLength();
        }
        Statement stat = null;
        try {
            stat = conn.createStatement();

            // 创建表
            String sql = UpgradeHelper.getCreateSQL(type, this.name, columnNames, columnTypes, columnNullAbles, primaryKeys, columnLengths, identityKeys);
            stat.executeUpdate(sql);
            // 添加表描述
            sql = UpgradeHelper.getAddTableCommentSQL(type, this.name, this.comment);
            stat.executeUpdate(sql);
            // 添加字段描述
            List<String> sqls = UpgradeHelper.getAddColumnCommentSQL(type, this.name, columnNames, columnComments, columnTypes, columnNullAbles, columnLengths, identityKeys);
            Iterator var11;
            if (sqls != null) {
                var11 = sqls.iterator();
                while(var11.hasNext()) {
                    String sql0 = (String)var11.next();
                    stat.addBatch(sql0);
                }
                stat.executeBatch();
            }
            UpgradeIndex index;
            var11 = this.indexList.iterator();
            while(var11.hasNext()) {
                index = (UpgradeIndex)var11.next();
                sql = UpgradeHelper.getAddIndexSQL(type, this.name, index.getName(), index.getColumnNames());
                log.debug(sql);
                stat.executeUpdate(sql);
            }

        } finally {
            if (stat != null) {
                try {
                    stat.close();
                } catch (SQLException var18) {
                }
            }

        }
    }

}
